package com.ahy231.controller;

import com.ahy231.dto.Result;
import com.ahy231.dto.ResultCode;
import com.ahy231.service.NewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ahy231
 * @date 2022/3/1 9:26
 * @description
 */
@Api(tags = "新闻")
@RestController
@RequestMapping("/news")
public class NewsController {

    private final NewsService newsService;

    public NewsController(NewsService newsService) {
        this.newsService = newsService;
    }

    @ApiOperation("获取新闻")
    @GetMapping("/get")
    @RequiresRoles("user")
    public Result getNews(@ApiParam("页面大小") @RequestParam Integer pageSize,
                          @ApiParam("页号") @RequestParam Integer pageNum) {
        try {
            return new Result(ResultCode.SUCCESS, newsService.getNews(pageSize, pageNum));
        } catch (Exception e) {
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }
}
